package com.cgsd.heyu.common;


import com.android.library.utils.BeanUtils;
import com.android.library.utils.SharedPreferencesHelper;
import com.android.library.utils.StringUtils;
import com.cgsd.heyu.entity.ConfigEntity;

import java.util.List;

/**
 * Description : 总配置
 * @author  : shaohua
 * 时间：2019/12/6 10
 * 邮箱：only_dream136@sina.com
 */
public class CommonConfig {
    private volatile static CommonConfig userModeConfig;

    /**
     * 1006
     * 分享文案提示
     */
    public static final String SHARE_COPYWRITING_HINT = "share_copywriting_hint";
    /**
     * 1006
     * 分享获得的积分数量
     */
    public static final String  SHARE_GIVE_FLOWER_NUM = "share_give_flower_num";
    /**
     * 1006
     * 新用户积分数
     */
    public static final String NEW_USER_GIVE_FLOWER_NUM = "new_user_give_flower_num";
    /**
     * 1011
     * 草根时代分享链接url
     * 此地址在草根时代详情分享中使用（还需添加参数）
     */
    public static final String DIARY_SHARE_H5_URL = "diary_share_h5_url";
    /**
     * 1003
     * 创建分类最大个数
     */
    public static final String USER_LABEL_NUM_KEY = "user_label_num_key";
    /**
     * 1004
     * 粉丝QQ群
     */
    public static final String FANS_QUN = "fans_qun";
    /**
     * 1004
     * 客户微信号
     */
    public static final String OFFICIAL_WECHART = "official_wechart";
    /**
     * 1002
     * 启动页开屏广告开关
     */
    public static final String OPEN_APP_ADVERTISING_ON_OFF = "open_app_advertising_on_off";
    /**
     * 1002
     * 积分提现开关
     */
    public static final String FLOWER_WITHDRAW_ON_OFF = "flower_withdraw_on_off";
    /**
     * 1002
     * 今日任务开关
     */
    public static final String DAY_TASK_ON_OFF = "day_task_on_off";
    /**
     * 1006
     * 发布草根时代奖励积分数
     */
    public static final String DAY_PUSH_DIARY_GIVE_FLOWER_NUM = "day_push_diary_give_flower_num";
    /**
     * 1011
     * 捐助页详情h5
     */
    public static final String FLOWER_RANKING_H5_URL = "flower_ranking_h5_url";
    /**
     * 1005
     * app分发链接地址
     */
    public static final String SHARE_DOWNLOAD_H5_URL = "share_download_h5_url";
    /**
     * 1002
     * 注销开关
     */
    public static final String UNSUBSCRIBE_ON_OFF = "unsubscribe_on_off";
    /**
     * 1003
     * 录音时长
     */
    public static final String VOICE_LENGTH = "voice_length";
    /**
     * 推广邀请链接
     */
    public static final String SHARE_INVITE_URL = "share_invite_h5_url";
    private static CommonConfig commonConfig;

    public CommonConfig(){}

    public static CommonConfig getInstance(){
        CommonConfig commonConfig = SharedPreferencesHelper.getBeanByFastJson(Constants.ShareKeyValue.COMMON_CONFIG,CommonConfig.class);
        if (commonConfig == null || StringUtils.isEmpty(commonConfig.downH5Url)){
            commonConfig = newInstance();
        }
        return commonConfig;
    }

    public static CommonConfig newInstance(){
        if (userModeConfig == null){
            synchronized (CommonConfig.class){
                if (userModeConfig == null){
                    userModeConfig = new CommonConfig();
                }
            }
        }
        return userModeConfig;
    }

    public static void setCommonConfig(CommonConfig commonConfig) {
        SharedPreferencesHelper.saveBeanByFastJson(Constants.ShareKeyValue.COMMON_CONFIG,commonConfig);
    }


    /**
     * 启动页开屏广告开关
     */
    public boolean isShowAd;
    /**
     * 积分提现开关
     */
    public boolean isShowCashOut;
    /**
     *第三方登录开关
     */
    public boolean isShowOtherLogin;
    /**
     * 今日任务开关
     */
    public boolean isShowDayTask;
    /**
     * 客户微信号
     */
    public String weixinNo;
    /**
     * 粉丝QQ群
     */
    public String qqGroup;
    /**
     * 查询捐助门槛列表
     */
    public List<ConfigEntity> donationNum;
    /**
     *查询捐助奖励文案
     */
    public String donationDes;
    /**
     * 分享获得的积分数量
     */
    public String shareRewordNum;
    /**
     * 新用户积分数
     */
    public String newUserReword;
    /**
     * 分享文案提示
     */
    public String shareDialogTitle;
    /**
     * 草根时代分享链接url
     * 此地址在草根时代详情分享中使用（还需添加参数）
     */
    public String shareUrl;
    /**
     * 创建分类最大个数
     */
    public String classifySize;
    /**
     * 发布草根时代奖励积分数
     */
    public String publishReword;
    /**
     * 捐助页详情h5
     */
    public String donateFlowerUrl;
    /**
     * app分发链接h5地址
     */
    public String downH5Url;
    /**
     * 注销按钮开关
     */
    public boolean isShowLogout;
    /**
     * 录音时长(秒)
     */
    public String voiceLength;
    /**
     * 分享推广链接
     */
    public String shareInviteUrl;


    /**
     * 设置数据
     * @param entities
     */
    public static void setCommonConfig(List<ConfigEntity> entities){
        if (BeanUtils.isEmpty(entities)){
            return;
        }
        commonConfig = new CommonConfig();
        for(ConfigEntity entity : entities){
            setCommonConfig(entity);
        }
        SharedPreferencesHelper.saveBeanByFastJson(Constants.ShareKeyValue.COMMON_CONFIG,commonConfig);

    }

    /**
     * 设置数据
     * @param entity
     */
    public static void setCommonConfig(ConfigEntity entity) {
        if(entity == null || commonConfig == null){
            return;
        }
        switch (entity.sysKey) {
            case SHARE_COPYWRITING_HINT:
                commonConfig.shareDialogTitle = entity.sysValue;
                break;
            case SHARE_GIVE_FLOWER_NUM:
                commonConfig.shareRewordNum = entity.sysValue;
                break;
            case NEW_USER_GIVE_FLOWER_NUM:
                commonConfig.newUserReword = entity.sysValue;
                break;
            case DIARY_SHARE_H5_URL:
                commonConfig.shareUrl = entity.sysValue;
                break;
            case USER_LABEL_NUM_KEY:
                commonConfig.classifySize = entity.sysValue;
                break;
            case FANS_QUN:
                commonConfig.qqGroup = entity.sysValue;
                break;
            case OFFICIAL_WECHART:
                commonConfig.weixinNo = entity.sysValue;
                break;
            case OPEN_APP_ADVERTISING_ON_OFF:
                commonConfig.isShowAd = "1".equals(entity.sysValue);
                break;
            case FLOWER_WITHDRAW_ON_OFF:
                commonConfig.isShowCashOut = "1".equals(entity.sysValue);
                break;
            case DAY_TASK_ON_OFF:
                commonConfig.isShowDayTask = "1".equals(entity.sysValue);
                break;
            case DAY_PUSH_DIARY_GIVE_FLOWER_NUM:
                commonConfig.publishReword = entity.sysValue;
                break;
            case FLOWER_RANKING_H5_URL:
                commonConfig.donateFlowerUrl = entity.sysValue + "userId="+UserModeConfig.getInstance().userId;
                break;
            case SHARE_DOWNLOAD_H5_URL:
                commonConfig.downH5Url = entity.sysValue;
                break;
            case UNSUBSCRIBE_ON_OFF:
                commonConfig.isShowLogout = "1".equals(entity.sysValue);
                break;
            case VOICE_LENGTH:
                commonConfig.voiceLength = entity.sysValue;
                break;
            case SHARE_INVITE_URL:
                commonConfig.shareInviteUrl = entity.sysValue;
                break;
            default:
        }

    }
}
